package com.sd.repay.controller;

import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.service.ActivateService;
import com.sd.repay.service.AgentService;
import com.sd.repay.utils.JsonUtil;
import com.sd.repay.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by sd on 2018/08/21
 * 激活码控制类
 */
@Controller
@RequestMapping(value = "/activateMgr")
public class ActivateController {

    private static final Logger log = LoggerFactory.getLogger(ActivateController.class);

    @Autowired
    private ActivateService activateService;
    @Autowired
    private AgentService agentService;

    /**
     * 生成激活码
     *
     * @return
     */
    @RequestMapping(value = "/generateActivateCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult generateActivateCode(HttpServletRequest request, @RequestParam Map<String, String> params) {

        log.info("生成激活码");
        String oemType = params.get("oemType");
        String lengthStr = params.get("length");

        try {
            if (StringUtil.isBlank(lengthStr)) {
                return CommonResult.build(403, "生成数量不能为空");
            }
            int length = Integer.parseInt(lengthStr);
            if (length < 1) {
                return CommonResult.build(403, "生成数量必须大于0");
            }
            //设置oem类型，默认为“default_repay”
            oemType = StringUtil.isBlank(oemType) ? Constants.DEFAULT_OEM_TYPE : oemType;
            params.put("oemType", oemType);

            boolean result = activateService.genertateActivateCode(length, oemType);
            if (!result) {
                return CommonResult.build(403, "生成激活码失败，请稍候再试");
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("生成激活码异常：" + e);
            return CommonResult.build(403, "生成激活码失败，请稍候再试");
        }
        return CommonResult.ok();
    }

    /**
     * 分配激活码
     *
     * @return
     */
    @RequestMapping(value = "/allotActivateCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult allotActivateCode(HttpServletRequest request, @RequestParam Map<String, String> params) {

        log.info("分配激活码");
        String beginIndexStr = params.get("beginIndex");
        String endIndexStr = params.get("endIndex");
        String agentNo = params.get("agentNo");

        try {
            if (StringUtil.isBlank(agentNo)) {
                return CommonResult.build(403, "分配代理商不能为空");
            }

            if (StringUtil.isBlank(beginIndexStr)) {
                return CommonResult.build(403, "激活码开始编号不能为空");
            }
            int beginIndex = Integer.parseInt(beginIndexStr);
            if (beginIndex < 1) {
                return CommonResult.build(403, "激活码开始编号必须大于0");
            }

            if (StringUtil.isBlank(endIndexStr)) {
                return CommonResult.build(403, "激活码结束编号不能为空");
            }
            int endIndex = Integer.parseInt(endIndexStr);
            if (endIndex < 1) {
                return CommonResult.build(403, "激活码结束编号必须大于0");
            }
            List<Map<String, Object>> allotList = activateService.getActivateByIndex(beginIndex, endIndex);
            if (CollectionUtils.isEmpty(allotList)) {
                return CommonResult.build(403, "此区间激活码信息不存在");
            }

            //校验代理商信息
            Map<String, Object> agentMap = agentService.getAgentInfoByAgentNo(agentNo);
            log.info("分配激活码代理商编号{}对应代理商信息{}", new Object[]{agentNo, JsonUtil.objectToJson(agentMap)});
            if (null == agentMap || agentMap.isEmpty()) {
                return CommonResult.build(403, "代理商不存在");
            }
            String oneAgentNo = StringUtil.filterNull(agentMap.get("one_agent_no"));
            String agentNode = StringUtil.filterNull(agentMap.get("agent_node"));

            activateService.allotActivateCode(agentNo, agentNode, oneAgentNo, beginIndex, endIndex);

        } catch (Exception e) {
            e.printStackTrace();
            log.info("生成激活码异常：" + e);
            return CommonResult.build(403, "生成激活码失败，请稍候再试");
        }
        return CommonResult.ok();
    }
}
